# Locate HUNSPELL library
# This module exports the following targets
#
# HUNSPELL::HUNSPELL
#
# This module defines
#  HUNSPELL_FOUND, if false, do not try to link to HUNSPELL
#  HUNSPELL_LIBRARY
#  HUNSPELL_INCLUDE_DIR, where to find hunspell/*.h

FIND_PACKAGE(PkgConfig)

PKG_SEARCH_MODULE(PC_HUNSPELL hunspell libhunspell)

FIND_PATH(HUNSPELL_INCLUDE_DIR hunspell/hunspell.h
  HINTS
    ${HUNSPELL_DIR}
    $ENV{HUNSPELL_DIR}
    ${PC_HUNSPELL_INCLUDE_DIRS}
  PATH_SUFFIXES
    include
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw # Fink
    /opt/local # DarwinPorts
    /opt/csw # Blastwave
    /opt
)

FIND_LIBRARY(HUNSPELL_LIBRARY_RELEASE
  NAMES
    hunspell
    libhunspell
    hunspell-1.7
    hunspell-1.6
    hunspell-1.5
    hunspell-1.4
    hunspell-1.3
  HINTS
    ${HUNSPELL_DIR}
    $ENV{HUNSPELL_DIR}
    ${PC_HUNSPELL_LIBRARY_DIRS}
    ${PC_HUNSPELL_LIBRARY_DIR}
  PATH_SUFFIXES
    lib64
    lib
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
)

FIND_LIBRARY(HUNSPELL_LIBRARY_DEBUG
  NAMES
    hunspelld
    libhunspelld
    hunspelld-1.7
    hunspelld-1.6
    hunspelld-1.5
    hunspelld-1.4
    hunspelld-1.3
  HINTS
    ${HUNSPELL_DIR}
    $ENV{HUNSPELL_DIR}
    ${PC_HUNSPELL_LIBRARY_DIRS}
    ${PC_HUNSPELL_LIBRARY_DIR}
  PATH_SUFFIXES
    lib64
    lib
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
)

IF(HUNSPELL_LIBRARY_DEBUG AND HUNSPELL_LIBRARY_RELEASE)
  SET(HUNSPELL_LIBRARY optimized ${HUNSPELL_LIBRARY_RELEASE} debug ${HUNSPELL_LIBRARY_DEBUG} )
ELSEIF(HUNSPELL_LIBRARY_RELEASE)
  SET(HUNSPELL_LIBRARY ${HUNSPELL_LIBRARY_RELEASE} )
ELSEIF(HUNSPELL_LIBRARY_DEBUG)
  SET(HUNSPELL_LIBRARY ${HUNSPELL_LIBRARY_DEBUG} )
ENDIF()

IF(PC_HUNSPELL_hunspell_FOUND)
  SET(HUNSPELL_VERSION ${PC_HUNSPELL_hunspell_VERSION})
ELSEIF(PC_HUNSPELL_libhunspell_FOUND)
  SET(HUNSPELL_VERSION ${PC_HUNSPELL_libhunspell_VERSION})
ELSE()
  SET(HUNSPELL_VERSION ${PC_HUNSPELL_VERSION})
ENDIF()

INCLUDE(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set HUNSPELL_FOUND to TRUE if
# all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(HUNSPELL
  REQUIRED_VARS
    HUNSPELL_LIBRARY
    HUNSPELL_INCLUDE_DIR
  VERSION_VAR
    HUNSPELL_VERSION
)

MARK_AS_ADVANCED(HUNSPELL_INCLUDE_DIR HUNSPELL_LIBRARY HUNSPELL_LIBRARY_RELEASE HUNSPELL_LIBRARY_DEBUG)

GET_FILENAME_COMPONENT(HUNSPELL_FILENAME ${HUNSPELL_LIBRARY} NAME_WE)
STRING(FIND ${HUNSPELL_FILENAME} .a HUNSPELL_STATIC)

IF(HUNSPELL_FOUND AND NOT TARGET HUNSPELL::HUNSPELL)
  IF(HUNSPELL_STATIC EQUAL -1)
    ADD_LIBRARY(HUNSPELL::HUNSPELL SHARED IMPORTED)
    SET_TARGET_PROPERTIES(HUNSPELL::HUNSPELL PROPERTIES
      IMPORTED_LOCATION
        "${HUNSPELL_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES
        "${HUNSPELL_INCLUDE_DIR}"
    )
  ELSE()
    ADD_LIBRARY(HUNSPELL::HUNSPELL STATIC IMPORTED)
    SET_TARGET_PROPERTIES(HUNSPELL::HUNSPELL PROPERTIES
      INTERFACE_COMPILE_DEFINITIONS
        HUNSPELL_STATIC
      IMPORTED_LOCATION
        "${HUNSPELL_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES
        "${HUNSPELL_INCLUDE_DIR}"
    )
  ENDIF()
ENDIF()